<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>公告管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-input
          size="small"
          v-model="formInline.select"
          placeholder="输入公告标题或内容"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="search"
          >搜索</el-button
        >
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="handleEdit()"
          >添加</el-button
        >
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table
      id="pdfDom"
      size="small"
      :data="noticeData"
      highlight-current-row
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      style="width: 100%"
    >
      <el-table-column align="center" sortable prop="id" label="ID" width="60">
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="title"
        label="公告标题"
        width="180"
      >
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="content"
        label="公告内容"
        width="390"
      >
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="storeid"
        label="适用仓库"
        width="130"
      >
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="createtime"
        label="创建时间"
        width="140"
      >
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="updatetime"
        label="更新时间"
        width="140"
      >
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="230">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="deleteUser(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination>
    <!-- 编辑界面 -->
    <el-dialog
      :title="title"
      :visible.sync="editFormVisible"
      width="35%"
      @click="closeDialog('edit')"
    >
      <el-form label-width="80px" ref="editForm" :model="editForm">
        <el-form-item label="标题" prop="title">
          <el-input
            size="small"
            v-model="editForm.title"
            auto-complete="off"
            placeholder="请输入公告标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input
            type="textarea"
            size="small"
            v-model="editForm.content"
            auto-complete="off"
            placeholder="请输入公告内容"
          ></el-input>
        </el-form-item>
        <el-form-item label="适用仓库" prop="storeid">
          <el-select
            clearable
            size="small"
            v-model="editForm.storeid"
            placeholder="请选择"
          >
            <el-option
              v-for="item in this.stores"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog('edit')">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          class="title"
          @click="submitForm('editForm')"
          >保存</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 导入请求方法
import {
  noticeList,
  noticeSave,
  noticeAdd,
  noticeDelete,
} from "../../api/sysMG";
import { getStroeItem } from "../../api/basisMG";
import Pagination from "../../components/Pagination";
import print from "vue-print-nb";
export default {
  data() {
    return {
      loading: false, //是显示加载
      title: "编辑公告",
      editFormVisible: false, //控制编辑页面显示与隐藏
      // 编辑与添加
      editForm: {
        id: "",
       title:"",
       content:"",
       storeid:""
      },
      // 请求数据参数
      formInline: {
        page: 1,
        size: 10,
      },
      // 仓库选择框数据源
      stores: [],
      // 公告数据
      noticeData: [],
      // 重置密码提交对象
      resetpwdparams: {},
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10,
      },
    };
  },
  // 注册组件
  components: {
    Pagination,
    print,
  },
  /**
   * 数据发生改变
   */
  watch: {},
  /**
   * 创建完毕
   */
  created() {
    this.getdata(this.formInline);
    this.getSelectItem();
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取仓库和角色列表数据
    getSelectItem() {
      getStroeItem().then((res) => {
        if (res.status == 200) {
          this.stores = res.data;
        } else {
          this.$message({
            type: "info",
            message: "获取仓库列表失败,刷新页面试试",
          });
        }
      });
    },
    // 获取数据方法
    getdata(parameter) {
      this.loading = true;
      // 获取公告列表
      noticeList(parameter).then((res) => {
        this.loading = false;
        if (res.status == 200) {
          this.noticeData = res.data;
          // 分页赋值
          this.pageparm.currentPage = this.formInline.page;
          this.pageparm.pageSize = this.formInline.size;
          this.pageparm.total = res.count;
        } else {
          this.$message({
            type: "info",
            message: res.statusInfo.message,
          });
        }
      });
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage;
      this.formInline.size = parm.pageSize;
      this.getdata(this.formInline);
    },
    //搜索事件
    search() {
      this.getdata(this.formInline);
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.editFormVisible = true;
      if (row != undefined && row != "undefined") {
        this.title = "修改公告";
        this.editForm.id = row.id;
        this.editForm.title = row.title;
        this.editForm.content = row.content;
        this.editForm.storeid = row.storeid;
      } else {
        this.title = "新增公告";
        this.editForm.id = "";
        this.editForm.title = "";
        this.editForm.content = "";
        this.editForm.storeid = "";
      }
    },
    // 编辑保存提交方法
    submitForm(editData) {
      this.$refs[editData].validate((valid) => {
        if (valid) {
          // id为空则提交为添加
          if (this.editForm.id == "") {
            noticeAdd(this.editForm)
              .then((res) => {
                this.editFormVisible = false;
                this.loading = false;
                if (res.status == 201) {
                  this.getdata(this.formInline);
                  this.$message({
                    type: "success",
                    message: "添加公告成功！",
                  });
                } else {
                  this.$message({
                    type: "error",
                    message: res.statusInfo.message,
                  });
                }
              })
              .catch((err) => {
                this.editFormVisible = false;
                this.loading = false;
                this.$message.error("保存失败，请稍后再试！");
              });
          } else {
            noticeSave(this.editForm)
              .then((res) => {
                this.editFormVisible = false;
                this.loading = false;
                if (res.status == 201) {
                  this.getdata(this.formInline);
                  this.$message({
                    type: "success",
                    message: "公告信息保存成功！",
                  });
                }
              })
              .catch((err) => {
                this.editFormVisible = false;
                this.loading = false;
                this.$message.error("保存失败，请稍后再试！");
              });
          }
        } else {
          return false;
        }
      });
    },
    // 关闭编辑、增加弹出框
    closeDialog(dialog) {
      if (dialog == "edit") {
        this.editFormVisible = false;
      }
    },
    // 删除公告
    deleteUser(index, row) {
      this.$confirm("确定要删除吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 删除
          noticeDelete({ id: row.id })
            .then((res) => {
              if (res.status == 201) {
                this.$message({
                  type: "success",
                  message: "公告已删除!",
                });
                this.getdata(this.formInline);
              } else {
                this.$message({
                  type: "error",
                  message: res.statusInfo.message,
                });
              }
            })
            .catch((err) => {
              this.loading = false;
              this.$message.error("数据删除失败，请稍后再试！");
            });
        })
        .catch(() => {});
    },
  },
};
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
</style>

 